package com.fh.permission.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fh.admin.entity.UmsAdmin;
import com.fh.permission.entity.UmsPermission;
import com.fh.permission.service.IUmsPermissionService;
import com.fh.result.ResultObject;
import com.fh.role.entity.UmsRole;
import com.fh.search.PermissionSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台用户权限表 前端控制器
 * </p>
 *
 * @author F
 * @since 2021-05-13
 */
@RestController
@RequestMapping("/ums/permission")
public class UmsPermissionController {

    @Autowired
    private IUmsPermissionService umsPermissionService;

    @GetMapping
    public IPage<UmsPermission>queryPermissionPageList(PermissionSearch permissionSearch){
        QueryWrapper<UmsPermission>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pid", permissionSearch.getPid());
        queryWrapper.orderByAsc("sort");
       return umsPermissionService.page(permissionSearch,queryWrapper);
    }

    @GetMapping("/parent")
    public ResultObject queryParentList(){
        QueryWrapper<UmsPermission>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pid", 0);
        List<UmsPermission> list=umsPermissionService.list(queryWrapper);
         return ResultObject.success(list);
    }
    @PostMapping
    public ResultObject saveOrUpdatePermission(@RequestBody UmsPermission umsPermission){
        umsPermissionService.saveOrUpdatePermission(umsPermission);
        return ResultObject.success();
    }
    @GetMapping("{roleId}")
    public ResultObject getRoleById(@PathVariable("roleId") Long roleId){
        UmsPermission role= umsPermissionService.getById(roleId);
        return ResultObject.success(role);
    }
    @DeleteMapping("/{roleId}")
    public ResultObject deleteRole(@PathVariable("roleId")Long roleId){
        umsPermissionService.removeById(roleId);
        return ResultObject.success();
    }
    @GetMapping("/son")
    public ResultObject querySonList(){
        QueryWrapper<UmsPermission>queryWrapper=new QueryWrapper<>();
        queryWrapper.notIn("pid", 0);
        List<UmsPermission> list=umsPermissionService.list(queryWrapper);
        return ResultObject.success(list);
    }

}
